

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>kospeech.metrics &mdash; KoSpeech 0.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> KoSpeech
          

          
          </a>

          
            
            
              <div class="version">
                0.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">NOTES</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../notes/intro.html">Intro</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/Preparation.html">Preparation before Training</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/opts.html">Options</a></li>
</ul>
<p class="caption"><span class="caption-text">ARCHITECTURE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../Seq2seq.html">Seq2seq</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Transformer.html">Transformer</a></li>
</ul>
<p class="caption"><span class="caption-text">PACKAGE REFERENCE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../Checkpoint.html">Checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Data.html">Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Decode.html">Decode</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Evaluator.html">Evaluator</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Optim.html">Optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Trainer.html">Trainer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Etc.html">Etc</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">KoSpeech</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>kospeech.metrics</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for kospeech.metrics</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">Levenshtein</span> <span class="k">as</span> <span class="nn">Lev</span>
<span class="kn">from</span> <span class="nn">kospeech.utils</span> <span class="k">import</span> <span class="n">label_to_string</span>


<div class="viewcode-block" id="ErrorRate"><a class="viewcode-back" href="../../Etc.html#kospeech.metrics.ErrorRate">[docs]</a><span class="k">class</span> <span class="nc">ErrorRate</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Provides inteface of error rate calcuation.</span>

<span class="sd">    Note:</span>
<span class="sd">        Do not use this class directly, use one of the sub classes.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">id2char</span><span class="p">,</span> <span class="n">eos_id</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">total_dist</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">total_length</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">id2char</span> <span class="o">=</span> <span class="n">id2char</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eos_id</span> <span class="o">=</span> <span class="n">eos_id</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">hypothesis</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Calculating character error rate &quot;&quot;&quot;</span>
        <span class="n">dist</span><span class="p">,</span> <span class="n">length</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_distance</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">hypothesis</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">total_dist</span> <span class="o">+=</span> <span class="n">dist</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">total_length</span> <span class="o">+=</span> <span class="n">length</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">total_dist</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">total_length</span>

    <span class="k">def</span> <span class="nf">_get_distance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">y_hats</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Provides total character distance between targets &amp; y_hats</span>

<span class="sd">        Args:</span>
<span class="sd">            targets (torch.Tensor): set of ground truth</span>
<span class="sd">            y_hats (torch.Tensor): predicted y values (y_hat) by the model</span>

<span class="sd">        Returns: total_dist, total_length</span>
<span class="sd">            - **total_dist**: total distance between targets &amp; y_hats</span>
<span class="sd">            - **total_length**: total length of targets sequence</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">total_dist</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">total_length</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="k">for</span> <span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="n">y_hat</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">targets</span><span class="p">,</span> <span class="n">y_hats</span><span class="p">):</span>
            <span class="n">s1</span> <span class="o">=</span> <span class="n">label_to_string</span><span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">id2char</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">eos_id</span><span class="p">)</span>
            <span class="n">s2</span> <span class="o">=</span> <span class="n">label_to_string</span><span class="p">(</span><span class="n">y_hat</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">id2char</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">eos_id</span><span class="p">)</span>

            <span class="n">dist</span><span class="p">,</span> <span class="n">length</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metric</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">)</span>

            <span class="n">total_dist</span> <span class="o">+=</span> <span class="n">dist</span>
            <span class="n">total_length</span> <span class="o">+=</span> <span class="n">length</span>

        <span class="k">return</span> <span class="n">total_dist</span><span class="p">,</span> <span class="n">total_length</span>

    <span class="k">def</span> <span class="nf">metric</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span></div>


<div class="viewcode-block" id="CharacterErrorRate"><a class="viewcode-back" href="../../Etc.html#kospeech.metrics.CharacterErrorRate">[docs]</a><span class="k">class</span> <span class="nc">CharacterErrorRate</span><span class="p">(</span><span class="n">ErrorRate</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Computes the Character Error Rate, defined as the edit distance between the</span>
<span class="sd">    two provided sentences after tokenizing to characters.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">id2char</span><span class="p">,</span> <span class="n">eos_id</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">CharacterErrorRate</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">id2char</span><span class="p">,</span> <span class="n">eos_id</span><span class="p">)</span>

<div class="viewcode-block" id="CharacterErrorRate.metric"><a class="viewcode-back" href="../../Etc.html#kospeech.metrics.CharacterErrorRate.metric">[docs]</a>    <span class="k">def</span> <span class="nf">metric</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the Character Error Rate, defined as the edit distance between the</span>
<span class="sd">        two provided sentences after tokenizing to characters.</span>

<span class="sd">        Arguments:</span>
<span class="sd">            s1 (string): space-separated sentence</span>
<span class="sd">            s2 (string): space-separated sentence</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">s1</span> <span class="o">=</span> <span class="n">s1</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="n">s2</span> <span class="o">=</span> <span class="n">s2</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>

        <span class="n">dist</span> <span class="o">=</span> <span class="n">Lev</span><span class="o">.</span><span class="n">distance</span><span class="p">(</span><span class="n">s2</span><span class="p">,</span> <span class="n">s1</span><span class="p">)</span>
        <span class="n">length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">s1</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">dist</span><span class="p">,</span> <span class="n">length</span></div></div>


<div class="viewcode-block" id="WordErrorRate"><a class="viewcode-back" href="../../Etc.html#kospeech.metrics.WordErrorRate">[docs]</a><span class="k">class</span> <span class="nc">WordErrorRate</span><span class="p">(</span><span class="n">ErrorRate</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Computes the Word Error Rate, defined as the edit distance between the</span>
<span class="sd">    two provided sentences after tokenizing to words.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">id2char</span><span class="p">,</span> <span class="n">eos_id</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">WordErrorRate</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">id2char</span><span class="p">,</span> <span class="n">eos_id</span><span class="p">)</span>

<div class="viewcode-block" id="WordErrorRate.metric"><a class="viewcode-back" href="../../Etc.html#kospeech.metrics.WordErrorRate.metric">[docs]</a>    <span class="k">def</span> <span class="nf">metric</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the Word Error Rate, defined as the edit distance between the</span>
<span class="sd">        two provided sentences after tokenizing to words.</span>

<span class="sd">        Arguments:</span>
<span class="sd">            s1 (string): space-separated sentence</span>
<span class="sd">            s2 (string): space-separated sentence</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># build mapping of words to integers</span>
        <span class="n">b</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">s1</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="o">+</span> <span class="n">s2</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
        <span class="n">word2char</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">b</span><span class="p">))))</span>

        <span class="c1"># map the words to a char array (Levenshtein packages only accepts</span>
        <span class="c1"># strings)</span>
        <span class="n">w1</span> <span class="o">=</span> <span class="p">[</span><span class="nb">chr</span><span class="p">(</span><span class="n">word2char</span><span class="p">[</span><span class="n">w</span><span class="p">])</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">s1</span><span class="o">.</span><span class="n">split</span><span class="p">()]</span>
        <span class="n">w2</span> <span class="o">=</span> <span class="p">[</span><span class="nb">chr</span><span class="p">(</span><span class="n">word2char</span><span class="p">[</span><span class="n">w</span><span class="p">])</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">s2</span><span class="o">.</span><span class="n">split</span><span class="p">()]</span>

        <span class="k">return</span> <span class="n">Lev</span><span class="o">.</span><span class="n">distance</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">w1</span><span class="p">),</span> <span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">w2</span><span class="p">))</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Soohwan Kim

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>